<?php session_start();
include_once ("../config/config.php"); 

Db_class::connection();


	

	if(isset($_POST['emailusername']) && isset($_POST['password'])){		
		$emailusername=$_POST['emailusername']; 
		$password=$_POST['password']; 
		$emailusername = stripslashes($emailusername);
		$password = stripslashes($password);
		$emailusername = mysql_real_escape_string($emailusername);
		$password = mysql_real_escape_string($password);
		$res = mysql_query("SELECT * FROM tblUser WHERE email = '$emailusername' OR username = '$emailusername' AND password= '$password'");
		$rows = mysql_fetch_assoc($res);
	
		if($rows){			
		switch($rows['user_type']):
			case 'admin':
				$_SESSION['user'] 	 = $rows['user_type'];
				$_SESSION['curuser'] = $rows['userid'];
				$_SESSION['us'] 	 = $rows['username'];
				$_SESSION['em'] 	 = $rows['email'];
				$_SESSION['dp'] 	 = $rows['avatar'];
				header("location:admin.php");
				break;
				
			case 'teacher':
				$_SESSION['user'] 	 = $rows['user_type'];
				$_SESSION['curuser'] = $rows['userid'];
				$_SESSION['us'] 	 = $rows['username'];
				$_SESSION['em'] 	 = $rows['email'];
				header("location:teacher.php");
				break;
				
			case 'student':
				$_SESSION['user'] 	 = $rows['user_type'];
				$_SESSION['curuser'] = $rows['userid'];
				$_SESSION['us'] 	 = $rows['username'];
				$_SESSION['em'] 	 = $rows['email'];
				$_SESSION['fn']		 = $rows['fname'];
				$_SESSION['ln']		 = $rows['lname'];
				header("location:student.php");
				break;
			
				
			case '':
				$_SESSION['user'] 	 = "";
				$_SESSION['curuser'] = "";
				$_SESSION['us'] 	 = "";
				$_SESSION['em'] 	 = "";
				
				
		print '
		
		<script type="text/javascript">
		alert("Username or password is incorrect!");
		window.location = "../login_r.php";
		</script>
		
		
		';
				break;
				
			endswitch;
		}
		else{
		print '		
				<script type="text/javascript">
		alert("Username or password is incorrect!"!");
		window.location = "../login_r.php";
		</script> ';
		}
	}
	
	
?>
